package com.xinzhitong.www.controller;

import com.xinzhitong.www.exception.QuestionMessageException;
import com.xinzhitong.www.exception.questionanalysis.NoQuestionAnalysisException;
import com.xinzhitong.www.exception.questionanalysis.TypeErrException;
import com.xinzhitong.www.exception.questionbank.QuestionBankEmptyException;
import com.xinzhitong.www.exception.questioncard.NoQuestionCardException;
import com.xinzhitong.www.exception.questionerr.NoErrQuestionException;
import com.xinzhitong.www.exception.questionerr.QuestionAnswerIsNullException;
import com.xinzhitong.www.exception.questionerrid.NoQuestionErrIdException;
import com.xinzhitong.www.exception.questioninformation.NoQuestionInformationException;
import com.xinzhitong.www.exception.questionmessage.NoQuestionMessageException;
import com.xinzhitong.www.exception.questionsign.InvalidSignOfTheQuestionException;
import com.xinzhitong.www.exception.questionsign.MultiSignOfSameQuestionException;
import com.xinzhitong.www.exception.test.EmptyTestOfTheCredentialException;
import com.xinzhitong.www.exception.test.EmptyTestOfTheProfessionException;
import com.xinzhitong.www.service.QuestionBankService;
import com.xinzhitong.www.utils.Message;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 题库控制器
 *
 * @author 徐鑫 钟伟良 魏骆淳
 */
@CrossOrigin
@RestController
@RequestMapping("questionBank")
public class QuestionBankController {

    @Resource
    QuestionBankService questionBankService;

    /**
     * 分页获取某工种下的题库概览
     *
     * @param professionId 工种id
     * @param pageNo       页码
     * @param pageSize     页大小
     * @return 题库概览
     */
    @RequestMapping(value = "questionBanks", method = RequestMethod.GET)
    public Message questionBanks(
            @RequestParam("professionId") Integer professionId,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize
    ) {
        String msg;
        try {
            return Message.createSuc(questionBankService.getQuestionBanks(professionId, pageNo, pageSize));
        } catch (QuestionBankEmptyException e) {
            e.printStackTrace();
            msg = "未获取到题库信息";
        }
        return Message.createErr(msg);
    }

    /***
     * 判断题目对错
     *
     * @param questionId 题目id
     * @param questionType 题目类型
     * @param userQuestionAnswer 用户做题答案
     * @return 题目答案
     */
    @RequestMapping(value = "getAnswerIsCorrect", method = RequestMethod.POST)
    public Message answerIsCorrect(
            @RequestParam("questionId") Integer questionId,
            @RequestParam("questionType") String questionType,
            @RequestParam("userQuestionAnswer") List<String> userQuestionAnswer
    ) {
        try {
            return Message.createSuc(questionBankService.getAnswerIsCorrect(questionId, questionType, userQuestionAnswer));
        } catch (QuestionAnswerIsNullException e) {
            e.printStackTrace();
            return Message.createErr("题目答案为空");
        }
    }

    /**
     * 获取用户做题信息
     *
     * @param id 题库id
     * @return 做题信息
     */
    @RequestMapping(value = "questionInformationGeneral", method = RequestMethod.GET)
    public Message questionInformation(@RequestParam(value = "id") Integer id) {
        return Message.createSuc(questionBankService.getQuestionInformationGeneral(id));
    }

    /**
     * 获取题目答案
     *
     * @param id   题目id
     * @param type 题目类型
     * @return 题目答案，解析，正确选项
     */
    @RequestMapping(value = "questionAnalysis", method = RequestMethod.GET)
    public Message questionAnalysis(@RequestParam(value = "id") Integer id,
                                    @RequestParam(value = "type") String type
    ) {
        try {
            return Message.createSuc(questionBankService.getQuestionAnalysis(id, type));
        } catch (NoQuestionAnalysisException e) {
            e.printStackTrace();
            return Message.createErr("此题目没有答案");
        } catch (TypeErrException e) {
            e.printStackTrace();
            return Message.createErr("题目号与类型不符");
        }
    }

    /**
     * 获取某证书下的测试题
     *
     * @param credentialId 证书id
     * @return 测试信息及题型题号
     */
    @RequestMapping(value = "randomTestOfCredentialIdOf", method = RequestMethod.GET)
    public Message randomTestOfCredentialIdOf(@RequestParam("credentialId") Integer credentialId) {
        String msg;
        try {
            return Message.createSuc(questionBankService.getTestOfCredentialIdOf(credentialId));
        } catch (EmptyTestOfTheCredentialException e) {
            e.printStackTrace();
            msg = "此证书下暂无测试，正在建设~";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取工种下的所有证书的测试概览
     *
     * @param professionId 工种id
     * @return 测试概览
     */
    @RequestMapping(value = "testsOfProfessionOf", method = RequestMethod.GET)
    public Message testsOfProfessionOf(@RequestParam("professionId") Integer professionId) {
        String msg;
        try {
            return Message.createSuc(questionBankService.getTestsOfProfessionOf(professionId));
        } catch (EmptyTestOfTheProfessionException e) {
            e.printStackTrace();
            msg = "工种下无测试，正在努力建设中";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取错题
     *
     * @return 我的错题
     */
    @RequestMapping(value = "questionErr", method = RequestMethod.GET)
    public Message questionErr() {
        try {
            return Message.createSuc(questionBankService.getQuestionErr());
        } catch (NoErrQuestionException e) {
            e.printStackTrace();
            return Message.createErr("暂无错题");
        }
    }

    /**
     * 获取题目
     *
     * @param bankId   题库id
     * @param type     题目类型
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return 题目列表
     */
    @RequestMapping(value = "questionGet", method = RequestMethod.GET)
    public Message questionGet(
            @RequestParam(value = "bankId") Integer bankId,
            @RequestParam(value = "type") String type,
            @RequestParam("pageNo") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ) {
        try {
            return Message.createSuc(questionBankService.getQuestionInformation(bankId, type, pageNo, pageSize));
        } catch (NoQuestionInformationException e) {
            return Message.createErr("此题库不存在");
        }
    }

    /**
     * 标记指定的题目
     *
     * @param id 题目id
     * @return 响应信息
     */
    @RequestMapping(value = "questionSign", method = RequestMethod.PUT)
    public Message questionSign(@RequestParam("id") Integer id) {
        String msg = "标记成功";
        try {
            questionBankService.signTheQuestion(id);
            return Message.createSuc(msg);
        } catch (NoQuestionInformationException e) {
            e.printStackTrace();
            msg = "此题目已失效";
        } catch (MultiSignOfSameQuestionException e) {
            e.printStackTrace();
            msg = "您已标记过该题";
        }
        return Message.createErr(msg);
    }

    /**
     * 移除指定题目的标记
     *
     * @param id 题目id
     * @return 响应信息
     */
    @RequestMapping(value = "questionSignRemove", method = RequestMethod.DELETE)
    public Message questionSignRemove(@RequestParam("id") Integer id) {
        String msg = "移除标记成功";
        try {
            questionBankService.removeSignOfTheQuestion(id);
            return Message.createSuc(msg);
        } catch (NoQuestionInformationException e) {
            e.printStackTrace();
            msg = "此题目已失效";
        } catch (InvalidSignOfTheQuestionException e) {
            e.printStackTrace();
            msg = "您尚未标记该题";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取做错了的题目
     *
     * @return 做错了的题目id
     */
    @RequestMapping(value = "getErrQuestion", method = RequestMethod.GET)
    public Message getErrQuestion() {
        try {
            return Message.createSuc(questionBankService.getErr());
        } catch (NoQuestionErrIdException e) {
            e.printStackTrace();
            return Message.createErr("没有错题");
        }
    }

    /**
     * 获取错题题面
     *
     * @param id 题目id
     * @return 错题题面
     */
    @RequestMapping(value = "questionErrGet", method = RequestMethod.GET)
    public Message questionGet(
            @RequestParam(value = "id") Integer id
    ) {
        try {
            return Message.createSuc(questionBankService.getErrQuestionInformation(id));
        } catch (NoQuestionInformationException e) {
            e.printStackTrace();
            return Message.createErr("此题目不存在");
        }
    }

    /**
     * 获取题库题面
     *
     * @param id 题目id
     * @return 题库题面
     */
    @RequestMapping(value = "questionBankTheme", method = RequestMethod.GET)
    public Message getQuestionBankTheme(
            @RequestParam(value = "id") Integer id
    ) {
        try {
            Map<String, Object> map = questionBankService.getErrQuestionInformation(id);
            List<String> userQuestionAnswer = questionBankService.getUserQuestionAnswer(id);
            map.put("userQuestionAnswer", userQuestionAnswer);
            return Message.createSuc(map);
        } catch (NoQuestionInformationException e) {
            e.printStackTrace();
            return Message.createErr("此题目不存在");
        }
    }

    /**
     * 获取答题卡
     *
     * @param bankId 题库id
     * @return 答题卡信息
     */
    @RequestMapping(value = "questionBankCard", method = RequestMethod.GET)
    public Message getQuestionBankCard(
            @RequestParam(value = "bankId") Integer bankId,
            @RequestParam(value = "type") String type
    ) {
        try {
            return Message.createSuc(questionBankService.getQuestionCard(bankId, type));
        } catch (NoQuestionCardException e) {
            e.printStackTrace();
            return Message.createErr("答题卡无信息");
        }
    }

    /**
     * 获取系统题库消息详情
     *
     * @param id 题库id
     * @return 消息详情
     * @throws QuestionMessageException 题库消息异常
     */
    @RequestMapping(value = "message", method = RequestMethod.GET)
    @ResponseBody
    public Message message(@RequestParam(value = "id") Integer id) throws QuestionMessageException {
        try {
            return Message.createSuc(questionBankService.getMessage(id));
        } catch (NoQuestionMessageException e) {
            e.printStackTrace();
            return Message.createErr("暂无消息");
        }
    }

}
